package com.service;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.icim.service.ICIMBaseService;
import com.tx.xpalminf.XpalminfI;
import com.tx.xpalminf.XpalminfO;
import com.tx.xpalminf.XpalminfOA;
import com.util.MqOpe;

@Scope("prototype")
@Service("XpalminfService")
public class XpalminfService  extends ICIMBaseService{
//	public class XpalminfService  extends AlarmDef implements ICIMService{
	private static Logger logger = Logger.getLogger(XpalminfService.class);
	
	private final char IN_GET_ALL ='A';
	private final char IN_GET_BY_TIME ='T';
	private final char IN_GET_ALM_CATE ='C';
	
	@Autowired
	private XpalminfI xpalminfI;
	
	@Autowired
	private XpalminfO xpalminfO; 
	
	@Autowired
	private MqOpe mqOpe;
	
	public String subMainProc(String strInMsg){
		try {
			xpalminfI = (XpalminfI) mqOpe.inStr2Obj(strInMsg,xpalminfI);
			logger.info("InTrx is "+ strInMsg );
			switch (xpalminfI.getAction_flg().toCharArray()[0])
			{
				case IN_GET_ALL: {
					xpalminfO = getAllAlmRecord(xpalminfI.getAlm_cate());
					xpalminfO.setAlm_cate(xpalminfI.getAlm_cate());					
					break;
				}
				case IN_GET_BY_TIME: {
					xpalminfO = getAllAlmRecord(xpalminfI.getAlm_cate(), xpalminfI.getBegin_timestamp() );
					xpalminfO.setAlm_cate(xpalminfI.getAlm_cate());				
					break;
				}
				case IN_GET_ALM_CATE:{
					xpalminfO = getAllAlmCate();
					break;
				}
				default:{
					throw new Exception("Invalide type input: [" + xpalminfI.getAction_flg() + "]");
				}
			} 
			xpalminfO.setRtn_code("0000000");
		} catch (Exception e) {
			xpalminfO.setRtn_code("0000001");
			xpalminfO.setRtn_mesg(e.getMessage());
			return mqOpe.obj2Str(xpalminfO);
		}finally{
			xpalminfO.setTrx_id("XPALMINF");
			xpalminfO.setType_id("O");
		}
		
		logger.info(mqOpe.obj2Str(xpalminfO));
		return mqOpe.obj2Str(xpalminfO);
	}
	
	/**
	 * 获取此Alm Cate全部Alarm数据
	 * @param almCate
	 * @return
	 */
	public XpalminfO getAllAlmRecord(String almCate){
		XpalminfO xpalminfO = new XpalminfO();
		List<XpalminfOA> xpalminfOAList = new ArrayList<XpalminfOA>();
		Integer record_cnt = 0;

//		String sqlStrQ = " from Alm_record where alm_cate = '"+ almCate +"' order by evt_timestamp desc";
//		List<Alm_record> almRecordList =almRecoreDao.find( sqlStrQ );
//		for(Alm_record alm_record:almRecordList){
//			XpalminfOA xpalminfOA = new XpalminfOA();
//			xpalminfOA.setEvt_timestamp(alm_record.getEvt_timestamp());
//			xpalminfOA.setAlm_msg(alm_record.getAlm_msg());
//			
//			xpalminfOAList.add(xpalminfOA);
//			record_cnt++;
//		}
//		
//		xpalminfO.setOary(xpalminfOAList);
//		xpalminfO.setRecord_cnt(record_cnt);
//		
		return xpalminfO;
	}
	
	/**
	 * 获取此Alm Cate在brginTime之后 的新alarm数据
	 * @param almCate
	 * @param brginTime
	 * @return
	 */
	public XpalminfO getAllAlmRecord(String almCate, String beginTime){
		XpalminfO xpalminfO = new XpalminfO();
		List<XpalminfOA> xpalminfOAList = new ArrayList<XpalminfOA>();
		Integer record_cnt = 0;

//		String sqlStrQ = " from Alm_record where alm_cate = '"+ almCate +"' and evt_timestamp > '"+ beginTime +"' order by evt_timestamp desc";
//		List<Alm_record> almRecordList =almRecoreDao.find( sqlStrQ );
//		for(Alm_record alm_record:almRecordList){
//			XpalminfOA xpalminfOA = new XpalminfOA();
//			xpalminfOA.setEvt_timestamp(alm_record.getEvt_timestamp());
//			xpalminfOA.setAlm_msg(alm_record.getAlm_msg());
//			
//			xpalminfOAList.add(xpalminfOA);
//			record_cnt++;
//		}
		
		xpalminfO.setOary(xpalminfOAList);
		xpalminfO.setRecord_cnt(record_cnt);
		
		return xpalminfO;
	}
	
	/**
	 * 获取当前有设定的alarm cate
	 * @param almCate
	 * @return
	 */
	public XpalminfO getAllAlmCate(){
		XpalminfO xpalminfO = new XpalminfO();
//		List<XpalminfOA2> xpalminfOA2List = new ArrayList<XpalminfOA2>();
//		List<Alm_code> almCodeList = new ArrayList<Alm_code>();
//		Integer almCateCnt = 0;
//		
//		almCodeList = almCodeDao.loadAll();
//		for(Alm_code almCode: almCodeList){
//			XpalminfOA2 xpalminfOA2 = new XpalminfOA2();
//			xpalminfOA2.setAlm_cate(almCode.getAlm_cate());
//			xpalminfOA2.setAlm_dec(almCode.getAlm_dec());
//			xpalminfOA2List.add(xpalminfOA2);
//			almCateCnt++;
//		}
//		
//		xpalminfO.setOary2(xpalminfOA2List);
//		xpalminfO.setAlm_cate_cnt(almCateCnt);
//		
		return xpalminfO;
	}
	
	
}
